import 'package:flutter/material.dart';
import 'package:slwl/utils/http.dart';
import 'package:slwl/utils/toast.dart';

class MinePage extends StatefulWidget {
  const MinePage({super.key});

  @override
  State<MinePage> createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> {
  List list = ['车辆信息', '任务数据', '系统设置'];

  Map mineData = {};

  Future getMineData() async {
    try {
      var res = await http.get('/driver/users');
      print(res);
      if (res['code'] == 200) {
        setState(() {
          mineData = res['data'];
        });
      }
    } catch (e) {
      print(e);
      ToastUtils.showError(e.toString());
    }
  }

  @override
  void initState() {
    super.initState();
    getMineData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        height: double.infinity,
        child: Stack(
          children: [
            Container(
              width: double.infinity,
              height: 292,
              decoration: const BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('assets/images/mine_bg.png'),
                  fit: BoxFit.fill,
                ),
              ),
              child: Column(
                children: [
                  Container(
                    margin: EdgeInsets.only(top: 50),
                    width: 66,
                    height: 66,
                    child: mineData['avatar'] != null
                        ? Image.network(mineData['avatar'])
                        : Image.asset('assets/images/avatar.png'),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 8),
                    child: Text(
                      mineData['name'] ?? '未命名',
                      style: const TextStyle(
                        color: Colors.white,
                        fontSize: 18,
                      ),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 7),
                    child: Text(
                      '司机编号：${mineData['number']}',
                      style: const TextStyle(
                        color: Colors.white,
                        fontSize: 14,
                      ),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 4),
                    child: Text(
                      '手机号：${mineData['phone']}',
                      style: const TextStyle(
                        color: Colors.white,
                        fontSize: 14,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Positioned(
              top: 227,
              left: 0,
              right: 0,
              child: Column(
                children: [
                  Container(
                      margin: const EdgeInsets.only(left: 15, right: 15),
                      padding: const EdgeInsets.only(top: 13, bottom: 24),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Container(
                              alignment: Alignment.center,
                              child: const Text(
                                '-本月任务-',
                                style: TextStyle(
                                    color: Color(0xFF818181), fontSize: 14),
                              )),
                          Container(
                              margin: EdgeInsets.only(top: 20),
                              child: const Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceAround,
                                  children: [
                                    Expanded(
                                        child: Column(children: [
                                      Text(
                                        '18',
                                        style: TextStyle(
                                            fontSize: 18,
                                            color: Color(0xFF2A2929),
                                            fontWeight: FontWeight.w600),
                                      ),
                                      SizedBox(height: 13),
                                      Text(
                                        '任务总量',
                                        style: TextStyle(
                                          fontSize: 12,
                                          color: Color(0xFF2A2929),
                                        ),
                                      ),
                                    ])),
                                    Expanded(
                                        child: Column(children: [
                                      Text('16',
                                          style: TextStyle(
                                              fontSize: 18,
                                              color: Color(0xFF2A2929),
                                              fontWeight: FontWeight.w600)),
                                      SizedBox(height: 13),
                                      Text('完成任务量',
                                          style: TextStyle(
                                            fontSize: 12,
                                            color: Color(0xFF2A2929),
                                          )),
                                    ])),
                                    Expanded(
                                        child: Column(children: [
                                      Text('3897',
                                          style: TextStyle(
                                              fontSize: 18,
                                              color: Color(0xFF2A2929),
                                              fontWeight: FontWeight.w600)),
                                      SizedBox(height: 13),
                                      Text('运输里程（km）',
                                          style: TextStyle(
                                            fontSize: 12,
                                            color: Color(0xFF2A2929),
                                          )),
                                    ])),
                                  ])),
                        ],
                      )),
                  const SizedBox(height: 15),
                  Container(
                    width: double.infinity,
                    height: 180,
                    padding: const EdgeInsets.only(left: 17, right: 17),
                    margin: const EdgeInsets.only(left: 15, right: 15),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(8),
                    ),
                    child: ListView.separated(
                        itemBuilder: (context, index) => GestureDetector(
                              onTap: () {
                                if (index == 2) {
                                  Navigator.pushNamed(context, '/setting');
                                }
                              },
                              child: SizedBox(
                                height: 59,
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    Text(
                                      '${list[index]}',
                                      style: const TextStyle(
                                        fontSize: 16,
                                        color: Color(0xFF2A2929),
                                      ),
                                    ),
                                    const Icon(
                                      Icons.arrow_forward_ios,
                                      size: 20,
                                      color: Color(0xFF818181),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                        separatorBuilder: (context, index) => const Divider(
                              height: 1,
                            ),
                        itemCount: 3),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
